package com.dongjiantong.sys.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.redis.RedisUtil;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.sys.mapper.DicIllnessMapper;
import com.dongjiantong.sys.model.DicIllness;
import com.dongjiantong.sys.service.DicIllnessService;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cao
 * @create 2017-11-25 下午4:24
 **/

@Slf4j
@Service(version = "1.0.0")
public class DicIllnessServiceImpl extends ServiceMybatis<DicIllness> implements DicIllnessService {

    public static final String DicIllness_KEY = "DicIllness:";

    @Resource
    private DicIllnessMapper dicIllnessMapper;


    @Override
    public  DicIllness queryByCode(String code)
    {
        String key =DicIllness_KEY+ String.format("queryByCode.%d",code);
        DicIllness illness =  RedisUtil.getObject(key,DicIllness.class);

        if(illness!=null) {
            return illness;
        }
        else {


            Example example = new Example(DicIllness.class);

            Example.Criteria criteria= example.createCriteria();
            criteria.andEqualTo("illcode",code);


            illness = this.selectOne(example);



            if(illness!=null) {

                RedisUtil.setObject(key, illness);
            }

            return illness;
        }
    }

    @Override
    public List<DicIllness> queryByName(String name)
    {
        Example example = new Example(DicIllness.class);

        Example.Criteria criteria= example.createCriteria();
        criteria.andLike("illname",name);

        return dicIllnessMapper.selectByExample(example);
    }

    @Override
    public List<DicIllness> queryIllnessByAnimalType(Integer animalType)
    {
        Example example = new Example(DicIllness.class);

        Example.Criteria criteria= example.createCriteria();
        criteria.andEqualTo("animalType",animalType);

        return dicIllnessMapper.selectByExample(example);
    }

    @Override
   public List<DicIllness> searchIllness(Integer animalCode)
   {
       if(animalCode==null || animalCode==0)
       {
           return dicIllnessMapper.selectAll();
       }

       return queryIllnessByAnimalType(animalCode);
   }

   @Override
    public List<DicIllness> queryIllness(Integer animalCode,String name)
    {
        Example example = new Example(DicIllness.class);

        Example.Criteria criteria= example.createCriteria();
        criteria.andEqualTo("animalType",animalCode);

        criteria.andLike("illname",name);

        return dicIllnessMapper.selectByExample(example);
    }

   @Override
   public Response<Boolean> saveOrUpdateIllness(DicIllness dicIllness)
   {
       //保存

       if(dicIllness.getId()==null || dicIllness.getId()==0L) {
           return saveIllness(dicIllness);
       }
       else {
           //编辑只能编辑名称
           return updateIllness(dicIllness);
       }
   }

   private Response<Boolean> saveIllness(DicIllness dicIllness)
   {
       DicIllness dicIllness1 =   queryByCode(dicIllness.getIllcode());

       if(dicIllness1!=null && !StringUtil.isBlank(dicIllness1.getIllcode()))
       {
           return Response.fail(HttpCode.BAD_REQUEST,"code已经存在");
       }


       int Result =  dicIllnessMapper.insert(dicIllness);



       return  Response.ok(Result>0) ;
   }

   private Response<Boolean> updateIllness(DicIllness dicIllness)
   {
       DicIllness dicIllness1 = dicIllnessMapper.selectByPrimaryKey(dicIllness.getId());

       dicIllness1.setIllname(dicIllness.getIllname());


       int Result =  dicIllnessMapper.updateByPrimaryKey(dicIllness1);



       return  Response.ok(Result>0) ;
   }
}
